﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_DL_Project_ManageUserClassTaskList : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string CourseNum = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClassTasks voClassTask = new VOTOClassTasks();
    TOClassTasks objClassTask = new TOClassTasks();
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    VOTOClassTasksReply votaskreply = new VOTOClassTasksReply();
    TOClassTasksReply objtaskReply = new TOClassTasksReply();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.Course.Entity.VOTOCourse vocourse = new PowerCreator.LMS.Course.Entity.VOTOCourse();
    PowerCreator.LMS.Web.Course.Course objcourse = new PowerCreator.LMS.Web.Course.Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        //int CTID = 1 & CourseID = 44 & CID = 1 = 0;
        string ctid = "0";
        if (Request.QueryString["CID"] != null)
        {
            string cid = Request.QueryString["CID"].ToString();
            ViewState["CID"] = cid;
            if (cid.Length > 0)
            {
                voClass = objClass.Load(int.Parse(cid));
                lbl_class.Text = voClass.Name;
            }
        }
        if (Request.QueryString["CourseID"] != null)
        {
            string courseid = Request.QueryString["CourseID"].ToString();
            ViewState["CourseID"] = courseid;
            if (courseid.Length > 0)
            {
                vocourse = objcourse.LoadVOTCourse(int.Parse(courseid));
                lbl_Course.Text = vocourse.CourseName;
            }
            else
                lbl_Course.Text = "综合";
        }

        if (Request.QueryString["CTID"] != null)
        {
            ctid = Request.QueryString["CTID"].ToString();
            ViewState["CTID"] = ctid.ToString();
            //voClass = objClass.Load(CID);
            //ClassName = voClass.Name;
            BinderPG(ctid);
        }
    }



    private void BinderPG(string ctid)
    {
        if (ViewState["CTID"] != null && ViewState["CourseID"] != null & ViewState["CID"] != null)
        {
            PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
            PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            string search = string.Empty;
            if (login != null)
            {
                DataTable dt = new DataTable();
                if (ctid.Length > 0)
                {
                    search = "SELECT TO_UserClassTask.UserTask_ID,TO_UserClassTask.Title, TO_UserClassTask.Creator, TO_Learner.UserName, TO_Learner.Name FROM TO_Learner INNER JOIN TO_UserClassTask ON  TO_Learner.Learner_Id = TO_UserClassTask.Creator where TO_UserClassTask.Task_Id=" + ctid + "";
                    dt = ov.Query(search);
                    lbl_finishnum.Text = dt.Rows.Count.ToString();
                    dt.Columns.Add("ClassTasks_Id");
                    dt.Columns.Add("Course_ID");
                    dt.Columns.Add("Score");
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        dt.Rows[j]["ClassTasks_Id"] = ViewState["CTID"].ToString();
                        dt.Rows[j]["Course_ID"] = ViewState["CourseID"].ToString();
                        dt.Rows[j]["Score"] = getPerScore(dt.Rows[j]["UserTask_ID"].ToString());
                    }

                }
                rptNew.DataSource = dt;
                rptNew.DataBind();

            }
        }
    }

    private string getPerScore(string UserTask_ID)
    {
        string sql = "";
        int total = 0;
        int num = 0;
        string per = "--";
        DataTable dt = new DataTable();

        sql = " ReplyType=1 and UserTask_Id=" + UserTask_ID;
        num = objtaskReply.Count(sql);
        if (num > 0)
        {
            sql = "select sum(Point) as sum from TO_ClassTasksReply where " + sql;
            dt = ov.Query(sql);
            if (dt.Rows.Count == 1)
            {
                total = int.Parse(dt.Rows[0]["sum"].ToString());
                per = (total / num).ToString();
            }
        }

        return per;

    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            Label lab_ID = (Label)e.Item.FindControl("lab_ID");

            if (lab_ID.Text == "0")
            {
                lnkbtn_editor.Enabled = false;
            }
        }
    }




    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        VOTOClassTasks vo = new VOTOClassTasks();
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        Label lbl_CourseID = (Label)e.Item.FindControl("lbl_CourseID");
        Label lab_ID = (Label)e.Item.FindControl("lab_ID");
        Label lbl_UserTask_ID = (Label)e.Item.FindControl("lbl_UserTask_ID");

        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "UserClassTasks.aspx?UserTask_ID=" + lbl_UserTask_ID.Text + "&ClassID=" + ViewState["CID"].ToString() + "&CourseID=" + lbl_CourseID.Text + "&TaskID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

        }



    }


}
